package com.runachina.appversion;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Isay on 2016/11/29.
 */
public class QueryNewVersion extends BombQuery {


    /**
     * @return 写是否有新版本
     */
//    public static void writeVersionInfo(boolean newVersion){
//        new LocalCfg().writeCfg("new_version",newVersion);
//    }
//
//
//    /**
//     *
//     * @return 读是否有新版本
//     */
//    public static boolean readVersionInfo(){
//        return new LocalCfg().readCfgBoolean("new_version");
//    }


    public interface ResponseListener{
        void onResponse(AppVersion version);
        void onError(String error);
    }


    /**
     * 查询bmob中是否有新版本
     */
    public void query(final ResponseListener listener) {
        BmobQuery<AppVersion> query = getQuery();
        if(null == query){
            return;
        }
        query.setLimit(1);
        //query.addWhereEqualTo("ip", UserInfo.getInstance().getUserServerAddress());
       // writeVersionInfo(false);//保存本地
        query.findObjects(new FindListener<AppVersion>() {
            @Override
            public void done(List<AppVersion> object, BmobException e) {
                if( null == e){
                    listener.onResponse(object.get(0));
                }else{
                    listener.onError("error");
                }
            }
        });
    }




    /**
     * 发现新版本弹窗
     * @param context
     */
//    public static void showDialog(final Context context,String msg, final AppVersion version){
//        final AlertDialog dialog = new AlertDialog.Builder(context).create();
//        dialog.show();
//        Window window = dialog.getWindow();
//        View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal, null);
//        ((TextView) view.findViewById(R.id.title)).setText("发现新版本");
//        ((TextView) view.findViewById(R.id.tv0)).setText(msg);
//        //按钮
//        Button positiveBtn = (Button) view.findViewById(R.id.positive_button);
//        positiveBtn.setText("立即更新");
//        Button negativeBtn = (Button) view.findViewById(R.id.negtive_button);
//        negativeBtn.setText("以后再说");
//        positiveBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//                Intent intent = new Intent(context,AppVersionActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putParcelable(AppVersionActivity.NEW_VERSION_KEY,version);
//                intent.putExtras(bundle);
//                context.startActivity(intent);
//            }
//        });
//        negativeBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        //弹窗大小
//        dialog.setCanceledOnTouchOutside(false);
//        WindowManager.LayoutParams params = window.getAttributes();
//        params.width = LinearLayout.LayoutParams.MATCH_PARENT;
//        params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//        window.setAttributes(params);
//        window.setContentView(view);
//    }

}
